package fakedata

import (
	"doctor_api_backend/config"
	"doctor_api_backend/models"
	"fmt"
	"math/rand"
	"time"

	"github.com/google/uuid"
)

// 药物信息也是写死的，不能随机模拟

// 生成随机日期
func generateRandomDateForMedicine() *time.Time {
	year := rand.Intn(5) + 2021 // 生成未来 5 年内的日期
	month := time.Month(rand.Intn(12) + 1)
	day := rand.Intn(28) + 1
	date := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
	return &date
}

// 生成唯一名称
func generateUniqueName() string {
	names := []string{"阿莫西林", "布洛芬", "头孢", "对乙酰氨基酚", "维生素C"}
	baseName := names[rand.Intn(len(names))]
	return fmt.Sprintf("%s-%s", baseName, uuid.New().String()[:8]) // 生成唯一后缀
}

// 生成药物假数据
func SeedMedicineData() {
	categories := []string{"中药", "西药"}
	descriptions := []string{"用于治疗感染", "用于缓解疼痛", "用于预防疾病"}
	dosageForms := []string{"片剂", "胶囊", "注射液"}
	strengths := []string{"100mg", "200mg", "500mg"}
	manufacturers := []string{"厂家A", "厂家B", "厂家C"}

	for i := 0; i < 100; i++ {
		medicine := models.Medicine{
			MedicineCode: fmt.Sprintf("M%d", i),
			Name:         generateUniqueName(),
			Category:     categories[rand.Intn(len(categories))],
			ICD10Code:    fmt.Sprintf("A%d", i),
			Description:  descriptions[rand.Intn(len(descriptions))],
			DosageForm:   dosageForms[rand.Intn(len(dosageForms))],
			Strength:     strengths[rand.Intn(len(strengths))],
			Manufacturer: manufacturers[rand.Intn(len(manufacturers))],
			Price:        rand.Float64() * 100, // 生成随机价格
			StockLevel:   rand.Intn(1000),      // 生成随机库存
			ExpiryDate:   generateRandomDateForMedicine(),
		}

		config.DB.Create(&medicine)
	}

	fmt.Println("100 条假 `药品`数据已成功插入数据库")
}
